#include <iostream>
#include <memory>
#include <string>
using namespace std;
class RaiseRequest{
public:
RaiseRequest(const string&name,int salary)
:_name(name)
,_salary(salary)
{
    cout << "RaiseRequest(const string &,int)" << endl;
}
const string & getName()const{
    return _name;
}
int getSalary()const {
    return _salary;
}
~RaiseRequest()
{
    cout << "~RaiseRequest()" << endl;
}
private:
string _name;
int _salary;
};

//薪水审批者父类
class SalaryApprover{
    public:
    SalaryApprover()
    :_nextChain(nullptr){
        cout <<"SalaryApprover()" << endl;
    }
    //设置指向的职责链中的下一个审批者
    void setNextChain(SalaryApprover * nextChain){
        _nextChain = nextChain;
    }
    //抽象类作为基类，需要将析构函数设置为虚函数
    virtual ~SalaryApprover(){
        cout << "virtual ~SalaryApprover()" << endl;
    }
    //处理加薪请求
    virtual void processRequest(const RaiseRequest & req) =0;
    protected:
    void sendRequestToNextHandler(const RaiseRequest & req){
        if(_nextChain != nullptr){
            _nextChain->processRequest(req);
        }else{
            cout << req.getName() << "的加薪要求为："
            << req.getSalary() << "元，但无人能审批!"<<endl;
        }
    }
    private:
    SalaryApprover * _nextChain;
};
class DepartmentManager : public SalaryApprover
{
    public:
    DepartmentManager(){
        cout << "DepartmentManager()" << endl;
    }

    //处理加薪请求
    virtual void processRequest(const RaiseRequest &req){
        int salary = req.getSalary();
        if(salary <= 1000){
                        //如果自己能处理，则自己处理
            cout << req.getName() << "的加薪要求为：" 
                 << salary << "元，部门经理审批通过！" << endl;
        }
        else
        {
            //自己不能处理，尝试找链中的下个对象来处理
            sendRequestToNextHandler(req);
        }
    }
    ~DepartmentManager(){
        cout << "~DepartMentManaget()" << endl;
    }
};
//技术总监子类
class CTO 
: public SalaryApprover
{
public:
    CTO()
    {
        cout << "CTO()" << endl;
    }

    //处理加薪请求
    virtual void processRequest(const RaiseRequest& req)
    {
        int salary = req.getSalary();
        if (salary > 1000 && salary <= 5000)
        {
            //如果自己能处理，则自己处理
            cout << req.getName() << "的加薪要求为：" 
                 << salary << "元，技术总监审批通过！" << endl;
        }
        else
        {
            sendRequestToNextHandler(req);//自己不能处理，尝试找链中的下个对象来处理
        }
    }

    ~CTO()
    {
        cout << "~CTO()" << endl;
    }
};

//总经理子类
class GeneralManager
: public SalaryApprover
{
public:
    GeneralManager()
    {
        cout << "GeneralManager()" << endl;
    }

    //处理加薪请求
    virtual void processRequest(const RaiseRequest& req)
    {
        int salary = req.getSalary();
        if (salary > 5000)
        {
            //如果自己能处理，则自己处理
            cout << req.getName() << "的加薪要求为：" 
                 << salary << "元，总经理审批通过！" << endl;
        }
        else
        {
            sendRequestToNextHandler(req);//自己不能处理，尝试找链中的下个对象来处理
        }
    }

    ~GeneralManager()
    {
        cout << "~GeneralManager()" << endl;
    }
};
void test(){
    unique_ptr<SalaryApprover> pdm(new DepartmentManager());
    unique_ptr<SalaryApprover> pcto(new CTO());
    unique_ptr<SalaryApprover> pgm(new GeneralManager());

    pdm->setNextChain(pcto.get());
    pcto->setNextChain(pgm.get());
    pgm->setNextChain(nullptr);

    RaiseRequest zs("zs",15000);
    RaiseRequest ls("ls",3500);
    RaiseRequest ww("ww",800);
    
    pdm->processRequest(zs);
    pdm->processRequest(ls);
    pdm->processRequest(ww);
}
int main()
{
    test();
    return 0;
}

